home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 56194 / 56194.xpi / chrome / topsite.jar / content / js / common.js < prev    next >
Text File  |  2010-01-17  |  5KB  |  133 lines

  1. if (!topsite) var topsite = {};
  2. if (!topsite.consts) topsite.consts = {};
  3. if (!topsite.common) topsite.common = {};
  4.  
  5. topsite.consts = {
  6.     
  7.     IS_DEBUG_MODE: false,
  8.     STRING_BUNDLE: Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService)
  9.                                     .createBundle("chrome://topsite/locale/topsite.properties"),
  10.     
  11.     URL_MAIN_LB: "http://similarnet.com/LoadBalancer/Main.asmx",
  12.     URL_MAIN_LB_BAK: "http://similarweb.net/LoadBalancer/Main.asmx",
  13.     
  14.     DATA_WS_NS: "http://schemas.datacontract.org/2004/07/SimilarService",
  15.     XHTML_NS:   "http://www.w3.org/1999/xhtml",
  16.     XUL_NS:     "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
  17.  
  18.     // Site status
  19.     STATUS_OK           : "0",
  20.     STATUS_SOON         : "1",
  21.     STATUS_NOT_SOON     : "2",
  22.     STATUS_INVALID      : "3",
  23.     STATUS_NO_DATA      : "4",
  24.  
  25.     // Navigation types
  26.     NAV_TYPE_REGULAR                    : "500", 
  27.     NAV_TYPE_CLICKED                    : "511", 
  28.     NAV_TYPE_ADV                        : "512", 
  29.     
  30.  
  31.     // Client cache modes
  32.     CACHE_NONE          : "0", // No cache
  33.     CACHE_IMAGE_ONLY    : "1", // Cache images only
  34.     CACHE_PARTIAL       : "2", // Partial cache, full cache (save related sites and images, all data) - BUT still call server, only one way (for statistics)
  35.     CACHE_FULL          : "4", // Full Cache - no calls to the server at all if the data is in the cache
  36.     CACHE_ON_DEMAND     : "5", // Full Cache - no calls to the server at all if the data is in the cache, and when in button mode - call to the server only on drop down
  37.  
  38.     HTTP_STATUS_OK : 200,
  39.     HTTP_STATUS_ACCEPTED : 202,
  40.  
  41.     // Preferences:
  42.  
  43.     // Second load balancer url
  44.     PREF_SECOND_LB : "extensions.topsite.secondLbURL",
  45.     PREF_G_SESSION : "extensions.topsite.gSession"
  46.  
  47. };
  48.  
  49. topsite.common = {
  50.     prefManager: Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch),
  51.     extMan: Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager),
  52.  
  53.     getAddonVersion: function(){        
  54.             var item = topsite.common.extMan.getItemForID("FirefoxAddon@topsite.com");        
  55.             return "TS:" + item.version;                
  56.         },
  57.     
  58.     S4: function() {
  59.             return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
  60.         },
  61.  
  62.         guid: function() {
  63.         with(topsite.common){
  64.             return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());        
  65.         }
  66.         },
  67.     
  68.         m_strGSessionId: null,
  69.     
  70.         getGSessionId: function(){            
  71.             if (topsite.common.m_strGSessionId == null){
  72.             try{
  73.                     topsite.common.m_strGSessionId = this.prefManager.getCharPref(topsite.consts.PREF_G_SESSION);                    
  74.             }catch(e){}
  75.                     if (!topsite.common.m_strGSessionId){
  76.                         topsite.common.m_strGSessionId = topsite.common.guid();                        
  77.                             topsite.common.prefManager.setCharPref(topsite.consts.PREF_G_SESSION, topsite.common.m_strGSessionId);
  78.                     }
  79.                 }
  80.                 
  81.             return topsite.common.m_strGSessionId;           
  82.         
  83.         },
  84.     
  85.         debug: function(msg, isFull){
  86.             if (!topsite.consts.IS_DEBUG_MODE)
  87.                 return;
  88.             var date = new Date();
  89.             var timeStr = date.getMinutes() + ":" + date.getSeconds() + ":" + date.getMilliseconds();
  90.             var prefix = "TS (" + timeStr + ") ";
  91.             try {
  92.                 a.b = b.c;
  93.             } catch (e) {
  94.                 if (isFull) {
  95.                     dump(prefix + ": " + e.stack + "\n");
  96.                     return;
  97.                 }
  98.                 var s = "chrome://topsite/content/";
  99.                 var index = e.stack.indexOf(s);
  100.                 index = e.stack.indexOf(s, index+1);
  101.                 var index2 = e.stack.indexOf("\n", index+1);
  102.                 var lineNumber = e.stack.substring(index+s.length, index2);
  103.                 dump(prefix + lineNumber + ": " + msg + "\n");
  104.                 // output to error console
  105.                 window.setTimeout(function() { throw new Error("[debug] " + msg); }, 0);
  106.             }
  107.         },
  108.  
  109.         keyHandler: function(event) {
  110.             if (event.keyCode==event.DOM_VK_RETURN) 
  111.                 topsite.overlay.findTopSite();
  112.         },
  113.  
  114.         openUrlFromDialog: function(url) {
  115.             var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  116.             var win = wm.getMostRecentWindow("navigator:browser");
  117.             var gBrowser = win.getBrowser();
  118.             gBrowser.loadOneTab(url, null, null, null, false, false);
  119.         },
  120.  
  121.         trim: function(str){
  122.             return str.replace(/^\s*/, "").replace(/\s*$/, "");
  123.         },
  124.         
  125.         capitalizeWords: function(string){
  126.             var words = string.split(" ");
  127.             string = "";
  128.             for each(word in words) {
  129.                 string = string + " " + word.substring(0, 1).toUpperCase() + word.substring(1, word.length);
  130.             }
  131.             return string.replace(/^\s+|\s+$/g,"");
  132.         }
  133. };